package com.weipeep.sys.controller;


import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.weipeep.common.base.BaseController;
import com.weipeep.common.config.Constant;
import com.weipeep.sys.service.UserProductService;
import com.weipeep.sys.vo.UserProductVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.weipeep.sys.domain.UserProductDO;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 用户和产品
 * </pre>
 * <small> 2018-06-23 13:25:51 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/userProduct")
public class UserProductController extends BaseController {
	@Autowired
	private UserProductService userProductService;
	
	@GetMapping()
	@RequiresPermissions("sys:userProduct:userProduct")
	String UserProduct(){
	    return "sys/userProduct/userProduct";
	}



	@GetMapping("/audit")
	@RequiresPermissions("sys:userProduct:audit")
	String UserProductAudit(){
		return "sys/userProduct/userProductAudit";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:userProduct:userProdunct")
	public Result<Page<UserProductDO>> list(Integer pageNumber, Integer pageSize, UserProductDO userProduct){
		// 查询列表数据
        Page<UserProductDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<UserProductDO> wrapper = new EntityWrapper<UserProductDO>(userProduct);
        page = userProductService.selectPage(page, wrapper);
        int total = userProductService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
	}
	@ResponseBody
	@GetMapping("/page")
	@RequiresPermissions("sys:userProduct:userProduct")
	public Result<Page<UserProductVo>> page(Integer pageNumber, Integer pageSize, UserProductVo userProduct){
		// 查询列表数据
		Long userId = getUser().getId();
		userProduct.setUserId(userId);
		Page<UserProductVo> page = new Page<>(pageNumber, pageSize);
		List<UserProductVo> userProductVoList = userProductService.selectUserProductList(userProduct,pageNumber,pageSize);
		int total = userProductService.selectUserProductCount(userProduct,pageNumber,pageSize);
        page.setRecords(userProductVoList);
		page.setTotal(total);
		return Result.ok(page);
	}

	@ResponseBody
	@GetMapping("/audit/page")
	@RequiresPermissions("sys:userProduct:audit")
	public Result<Page<UserProductVo>> auditPage(Integer pageNumber, Integer pageSize, UserProductVo userProduct){
		// 查询列表数据
		Page<UserProductVo> page = new Page<>(pageNumber, pageSize);
		List<UserProductVo> userProductVoList = userProductService.selectUserProductAuditList(userProduct,pageNumber,pageSize);
		int total = userProductService.selectUserProductAuditCount(userProduct,pageNumber,pageSize);
		page.setRecords(userProductVoList);
		page.setTotal(total);
		return Result.ok(page);
	}
	@GetMapping("/add")
	@RequiresPermissions("sys:userProduct:add")
	String add(){
	    return "sys/userProduct/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sys:userProduct:edit")
	String edit(@PathVariable("id") Long id,Model model){
		UserProductDO userProdunct = userProductService.selectById(id);
		model.addAttribute("userProdunct", userProdunct);
	    return "sys/userProduct/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:userProduct:add")
	public Result<String> save( UserProductDO userProduct){
		if(userProductService.insert(userProduct)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 保存或更新
	 */
	@ResponseBody
	@PostMapping("/saveOrUpdate")
	@RequiresPermissions(value = {"sys:userProduct:add","sys:userProduct:edit"},logical=Logical.AND)
	public Result<String> saveOrUpdate( UserProductDO userProduct){
		Long userId = getUserId();
		userProduct.setUserId(userId);
		Date now = Calendar.getInstance().getTime();
		if (userProduct.getId() !=null  &&  userProduct.getId()>0){
			userProduct.setUpdateTime(now);
			if(userProductService.updateById(userProduct)){
				return Result.ok();
			}
		}else {
			userProduct.setAuditStatus(Constant.ProductAuditStatus.NOT_AUDIT);
			userProduct.setCreateTime(now);
			userProduct.setUpdateTime(now);
			if(userProductService.insert(userProduct)){
				return Result.ok();
			}
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:userProduct:edit")
	public Result<String>  update( UserProductDO userProdunct){
		userProductService.updateById(userProdunct);
		return Result.ok();
	}
	/**
	 * 审核
	 */
	@ResponseBody
	@RequestMapping("/audit")
	@RequiresPermissions("sys:userProduct:audit")
	public Result<String>  audit( UserProductDO userProdunct){
		userProductService.updateById(userProdunct);
		return Result.ok();
	}
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions(value = {"sys:userProduct:remove","sys:userProduct:audit"},logical = Logical.OR)
	public Result<String>  remove( Long id){
		if(userProductService.deleteById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions(value = {"sys:userProduct:batchRemove","sys:userProduct:audit"},logical = Logical.OR)
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		userProductService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
